package com.cgtong.android.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cgtong.android.App;
import com.cgtong.android.R;
import com.cgtong.android.adapter.ProblemsAdapter;
import com.cgtong.android.base.BaseTitleActivity;
import com.cgtong.android.bean.ProblemBean;
import com.cgtong.android.callback.EmptyCallback;
import com.cgtong.android.callback.ErrorCallback;
import com.cgtong.android.mvp.contract.HisProblemsContract;
import com.cgtong.android.mvp.presenter.HisProblemsPresenter;
import com.cgtong.android.util.PostUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zhouyou.http.exception.ApiException;

import java.util.List;

import butterknife.BindView;

/**
 * 其他用户发布的案件
 */
public class HisProblemsActivity extends BaseTitleActivity implements HisProblemsContract.View {
    private static final String KEY_USER_ID = "user_id";
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipe_layout)
    SwipeRefreshLayout mSwipeLayout;
    private List<ProblemBean> mList;
    private ProblemsAdapter mAdapter;
    private HisProblemsPresenter mPresenter;

    private long mUserId;

    public static void start(Context context, long userId) {
        Intent starter = new Intent(context, HisProblemsActivity.class);
        starter.putExtra(KEY_USER_ID, userId);
        context.startActivity(starter);
    }

    @Override
    protected String getContentTitle() {
        return getString(R.string.title_his_problems);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_his_problems;
    }

    @Override
    protected void loadDataByNet() {
        mPresenter = new HisProblemsPresenter(this);

        mUserId = getIntent().getLongExtra(KEY_USER_ID, 0);
        initRecyclerView();
        mPresenter.httpProblemsData(0, mUserId, true);

    }

    @Override
    protected void onDataReloadByNet(View v) {
        mPresenter.httpProblemsData(0, mUserId, true);
    }

    /**
     * 初始化列表
     */
    private void initRecyclerView() {
        mSwipeLayout.setColorSchemeResources(R.color.color_scheme);
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.httpProblemsData(0, mUserId, true);
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ProblemsAdapter(mList, this);
        mAdapter.setShowFollowIcon(false);
        mRecyclerView.setAdapter(mAdapter);
//        mRecyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL, R.drawable.shape_horizontal_divider));

        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        //跳转详情
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ProblemBean problemBean = mAdapter.getItem(position);
                boolean canDelete = false;
                boolean canEvaluate = false;
                if (((App) getApplicationContext()).isUserLogin()) {
                    if ((problemBean.getStatus() == 4 || problemBean.getStatus() == 5 || problemBean.getStatus() == 7) &&  App.getInstance().isOwnself(problemBean.getCreateUserId())) {
                        canDelete = true;
                    }
                    if ((problemBean.getStatus() == 4 || problemBean.getStatus() == 5) &&  App.getInstance().isOwnself(problemBean.getCreateUserId())) {
                        canEvaluate = true;
                    }
                }
                ProblemDetailActivity.start(HisProblemsActivity.this, problemBean.getId(), canDelete, canEvaluate);

            }
        });

//        mAdapter.setLoadMoreView(new CustomLoadMoreView());
        mAdapter.disableLoadMoreIfNotFullPage(mRecyclerView);
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                long maxId = mAdapter.getItem(mAdapter.getItemCount() - 2).getCreateUserId();
                mPresenter.httpProblemsData(maxId, mUserId, false);


            }
        }, mRecyclerView);
    }

    @Override
    public void setRefreshing(boolean isRefresh) {
        if(mSwipeLayout!=null){
            mSwipeLayout.setRefreshing(isRefresh);
        }
    }

    @Override
    public void setEnableLoadMore(boolean enableLoadMore) {
        mAdapter.setEnableLoadMore(enableLoadMore);
    }

    @Override
    public void onLoadMoreEnd(boolean isLoadMoreEnd) {
        mAdapter.loadMoreEnd(isLoadMoreEnd);
    }


    @Override
    public void onError(ApiException e) {
        PostUtil.postCallbackDelayed(loadService, ErrorCallback.class);
    }


    @Override
    public void onNullData() {
        PostUtil.postCallbackDelayed(loadService, EmptyCallback.class);
    }

    @Override
    public void onHttpProblemsDataSuccess(List<ProblemBean> datas, boolean isRefresh) {
        PostUtil.postSuccessDelayed(loadService);
        if (isRefresh) {
            mAdapter.setNewData(datas);
        } else {
            mAdapter.addData(datas);
        }
        mAdapter.loadMoreComplete();
    }
}
